/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.applis.demo.forms;

import opiam.admin.applis.demo.beans.Person;

import opiam.admin.faare.service.services.references.ReferenceElement;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;


/**
 * This class describes the person formBean.
 */
public class PersonForm extends ActionForm implements Serializable
{
    /** User object. */
    private Person user = new Person();

    /*
     * gestion de champ de type fichier:
     * - <attrib>File transporte le fichier recu en HTTP
     * - delete<Attrib> vaut "on" si il faut supprimer l'attribut, null sinon
     * ne pas oublier de mettre l'enctype dans le formulaire !!!
     */
    /** Photo file. */
    private FormFile photoFile = null;

    /** Photo action. */
    private String deletePhoto = null;

    /** Photo file. */
    private FormFile certificateFile = null;

    /** Photo action. */
    private String deleteCertificate = null;

    /** Title list. */
    private List titlesList = null;

    /** Application authorization list. */
    private List appAuthList = null;

    /** Application authorization values list. */
    private List appAuthValueList = null;

    /** Password confirmation. */
    private String confirmPassword = null;

    /** Password. */
    private String newPassword = null;

    /** Description multivalued field. */
    private List descrp = null;

    /** Departements list. */
    private List departmentsList = null;

    /** Sites list. */
    private List sitesList = null;

    /** Manager. */
    private String manager = null;

    /** DOCUMENT ME */
    private Person managerPerson = null;

    /** DOCUMENT ME */
    private String deleteManager = null;

    /**
     * Creates a new PersonForm object.
     */
    public PersonForm()
    {
        titlesList = new ArrayList();
        titlesList.add("Accountant");
        titlesList.add("Assistant");
        titlesList.add("Developer");
        titlesList.add("Domain Expert");
        titlesList.add("Executive President");
        titlesList.add("Product Developer");
        titlesList.add("Product Manager");
        titlesList.add("Project Manager");
        titlesList.add("Senior Consultant");
        titlesList.add("Service Manager");
        titlesList.add("Support Developer");
        titlesList.add("Team Manager");
        titlesList.add("Technical Architect");

        appAuthList = new ArrayList();
        appAuthList.add("No");
        appAuthList.add("Yes");

        appAuthValueList = new ArrayList();
        appAuthValueList.add("");
        appAuthValueList.add("on");
    }

    /**
     * Gets the deleteManager parameter.
     *
     * @return the deleteManager parameter.
     */
    public String getDeleteManager()
    {
        return deleteManager;
    }

    /**
     * Sets the deleteManager parameter.
     *
     * @param deleteManager The deleteManager parameter to set.
     */
    public void setDeleteManager(String deleteManager)
    {
        this.deleteManager = deleteManager;
    }

    /**
     * Gets the description(multi-valued attribute).
     * Used by the jsp page.
     *
     * @return the description.
     */
    public String getDescrp()
    {
        StringBuffer sb = new StringBuffer();

        if ((descrp != null) && (descrp.size() > 0))
        {
            for (int i = 0; i < descrp.size(); i++)
            {
                sb.append((String) descrp.get(i));
                sb.append("\n");
            }

            return sb.toString();
        }

        return "";
    }

    /**
     * Sets the dscription(multi-valued attribute).
     * Used by the jsp page.
     *
     * @param descrp  The description to set.
     */
    public void setDescrp(String adescrp)
    {
        if ((adescrp != null) && (adescrp.trim().length() > 0))
        {
            StringTokenizer stk = new StringTokenizer(adescrp, "\n");
            descrp = new ArrayList();

            while (stk.hasMoreTokens())
            {
                descrp.add(stk.nextToken());
            }
        }
        else
        {
            descrp = null;
        }
    }

    /**
     * Gets the description (multi-valued attribute).
     * Used by the person bean.
     */
    public Collection getDescription()
    {
        return descrp;
    }

    /**
     * Sets the description(multi-valued attribute).
     * Used by the person bean.
     *
     * @param ldescrp  The description to set.
     */
    public void setDescription(Collection ldescrp)
    {
        this.descrp = new ArrayList();

        if ((ldescrp != null) && (ldescrp.size() > 0))
        {
            this.descrp.addAll(ldescrp);
        }
    }

    /**
     * Gets the user.
     *
     * @return The user.
     */
    public Person getUser()
    {
        return user;
    }

    /**
     * Sets the user.
     *
     * @param user  The user to set.
     */
    public void setUser(Person user)
    {
        this.user = user;
    }

    /**
     * Reset all the value of the object.
     */
    public void reset()
    {
        user = null;
    }

    /**
     * Gets the deleteCertificate parameter.
     *
     * @return the deleteCertificate parameter.
     */
    public String getDeleteCertificate()
    {
        return deleteCertificate;
    }

    /**
     * Sets the deleteCertificate parameter.
     *
     * @param deleteCertificate  The deleteCertificate to set.
     */
    public void setDeleteCertificate(String deleteCertificate)
    {
        this.deleteCertificate = deleteCertificate;
    }

    /**
     * Gets the deletePhoto parameter.
     *
     * @return the deletePhoto parameter.
     */
    public String getDeletePhoto()
    {
        return deletePhoto;
    }

    /**
     * Sets the deletePhoto parameter.
     *
     * @param deletePhoto  The deletePhoto to set.
     */
    public void setDeletePhoto(String deletePhoto)
    {
        this.deletePhoto = deletePhoto;
    }

    /**
     * Gets the new passsword.
     *
     * @return the new password.
     */
    public String getNewPassword()
    {
        return newPassword;
    }

    /**
     * Sets the new passsword.
     *
     * @param newPassword  The new password to set.
     */
    public void setNewPassword(String newPassword)
    {
        this.newPassword = newPassword;
    }

    /**
     * Gets the photo file.
     *
     * @return the photo file content.
     */
    public FormFile getPhotoFile()
    {
        return photoFile;
    }

    /**
     * Sets the photo file.
     *
     * @param photoFile  The photo file to set.
     */
    public void setPhotoFile(FormFile photoFile)
    {
        this.photoFile = photoFile;
    }

    /**
     * Gets the titles list.
     *
     * @return the titles list.
     */
    public List getTitlesList()
    {
        return titlesList;
    }

    /**
     * Sets the titles list.
     *
     * @param titleList  The titles list to set.
     */
    public void setTitlesList(List titlesList)
    {
        this.titlesList = titlesList;
    }

    /**
     * Sets the titles list and puts the reference title 
     * at the first position.
     *
     * @param titleRef  The reference title.
     */
    public void setTitleRef(String titleRef)
    {
        ArrayList tList = new ArrayList();
        tList.add(titleRef);

        for (int i = 0; i < titlesList.size(); i++)
        {
            String title = (String) titlesList.get(i);

            if (!title.equals(titleRef))
            {
                tList.add(title);
            }
        }

        this.titlesList = tList;
    }

    /**
     * Gets the authorizations list.
     *
     * @return the authorizations list.
     */
    public List getAppAuthList()
    {
        return appAuthList;
    }

    /**
     * Sets the authorizations list.
     *
     * @param appAuthList  The authorization list to set.
     */
    public void setAppAuthList(List appAuthList)
    {
        this.appAuthList = appAuthList;
    }

    /**
     * Sets the authorizations list and puts the reference authorization
     * at the first position.
     * 
     * @param appAuthList  The reference authorization.
     */
    public void setAppAuthRef(String appAuthRef)
    {
        ArrayList authList = new ArrayList();
        ArrayList authValueList = new ArrayList();
        authList.add(appAuthRef);

        if (appAuthRef.equals("Yes"))
        {
            authValueList.add("on");
        }
        else
        {
            authValueList.add("");
        }

        for (int i = 0; i < appAuthList.size(); i++)
        {
            String auth = (String) appAuthList.get(i);

            if (!auth.equals(appAuthRef))
            {
                authList.add(auth);

                if (auth.equals("Yes"))
                {
                    authValueList.add("on");
                }
                else
                {
                    authValueList.add("");
                }
            }
        }

        this.appAuthList = authList;
        this.appAuthValueList = authValueList;
    }

    /**
     * Gets the certificate file.
     *
     * @return the certificate File content.
     */
    public FormFile getCertificateFile()
    {
        return certificateFile;
    }

    /**
     * Sets the certificate file.
     *
     * @param certificateFile  The certificate file to set.
     */
    public void setCertificateFile(FormFile certificateFile)
    {
        this.certificateFile = certificateFile;
    }

    /**
     * Gets the confirmation password.
     *
     * @return Returns the confirmation password.
     */
    public String getConfirmPassword()
    {
        return confirmPassword;
    }

    /**
     * Sets the confirmation password.
     *
     * @param confirmPassword  The confirmation password to set.
     */
    public void setConfirmPassword(String confirmPassword)
    {
        this.confirmPassword = confirmPassword;
    }

    /**
     * Gets the departments list.
     *
     * @return the departments list.
     */
    public List getDepartmentsList()
    {
        return departmentsList;
    }

    /**
     * Sets the departments list.
     *
     * @param departmentsList  The departments list to set.
     */
    public void setDepartmentsList(List departmentsList)
    {
        this.departmentsList = departmentsList;
    }

    /**
     * Sets the departments list with a reference department.
     *
     * @param departmentsList  The departments list to set.
     * @param department       The reference department.
     */
    public void setDepartments(List departmentsList, String department)
    {
        ArrayList deps = new ArrayList();
        deps.add(department);

        if (departmentsList != null)
        {
            for (int i = 0; i < departmentsList.size(); i++)
            {
                ReferenceElement refel =
                    (ReferenceElement) departmentsList.get(i);
                String dep = refel.getValue();

                if (!dep.equals(department))
                {
                    deps.add(dep);
                }
            }
        }

        this.departmentsList = deps;
    }

    /**
     * Gets the sites list.
     *
     * @return the sites list.
     */
    public List getSitesList()
    {
        return sitesList;
    }

    /**
     * Sets the sites list.
     *
     * @param sitesList  The sites list to set.
     */
    public void setSitesList(List sitesList)
    {
        this.sitesList = sitesList;
    }

    /**
     * Sets the sites list with a reference site.
     *
     * @param sitesList  The sites list to set.
     * @param siteRef    The reference site.
     */
    public void setSites(List sitesList, String siteRef)
    {
        ArrayList sites = new ArrayList();
        sites.add(siteRef);

        if (sitesList != null)
        {
            for (int i = 0; i < sitesList.size(); i++)
            {
                ReferenceElement refel = (ReferenceElement) sitesList.get(i);
                String site = refel.getValue();

                if (!site.equals(siteRef))
                {
                    sites.add(site);
                }
            }
        }

        this.sitesList = sites;
    }

    /**
     * Gets the manager.
     *
     * @return the manager.
     */
    public String getManager()
    {
        return manager;
    }

    /**
     * Sets the manager.
     *
     * @param manager  The manager to set.
     */
    public void setManager(String manager)
    {
        this.manager = manager;
    }

    /**
     * Gets the manager object.
     * Used by the person bean.
     *
     * @return the manager object.
     */
    public Collection getManagerInPerson()
    {
        if (managerPerson != null)
        {
            Collection managers = new ArrayList();
            managers.add(managerPerson);

            return managers;
        }

        return null;
    }

    /**
     * Sets the manager object.
     * Used by the person form.
     *
     * @param managers  The manager object to set.
     */
    public void setManagerFromPerson(Collection managers)
    {
        if ((managers != null) && (managers.size() > 0))
        {
            Iterator iter = managers.iterator();
            Person manager = (Person) iter.next();
            this.manager = manager.getCommonName();
            this.managerPerson = manager;
        }
        else
        {
            this.manager = null;
        }
    }

    /**
     * Gets the manager object.
     * Used by the person form.
     *
     * @return the manager object.
     */
    public Person getManagerPerson()
    {
        return managerPerson;
    }

    /**
     * Sets the manager object.
     * Used by the person form.
     *
     * @param managerPerson  The manager to set.
     */
    public void setManagerPerson(Person managerPerson)
    {
        this.managerPerson = managerPerson;
    }

    /**
     * Gets the authorizations values list.
     *
     * @return the authorizations values list.
     */
    public List getAppAuthValueList()
    {
        return appAuthValueList;
    }

    /**
     * Sets the authorizations values list.
     *
     * @param appAuthValueList  The authorizations list to set.
     */
    public void setAppAuthValueList(List appAuthValueList)
    {
        this.appAuthValueList = appAuthValueList;
    }
}
